<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, maximum-scale=1.0,user-scalable=no,minimal-ui">
    <title>桔子❀</title>
    <link rel="icon" href="./../images/favico.ico">
    <!--element-ui的样式-->
    <link rel="stylesheet" href="../../backend/plugins/element-ui/index.css"/>
    <!-- 引入样式  -->
    <link rel="stylesheet" href="../styles/index.css"/>
    <!--引入vant样式-->
    <link rel="stylesheet" href="../styles/vant.min.css"/>
    <!--本页面内容的样式-->
    <link rel="stylesheet" href="./../styles/user.css"/>
</head>
<body>
<div id="user" class="app">
    <div class="divHead">
        <div class="divTitle">
            <i class="el-icon-arrow-left" @click="goBack"></i>个人中心
        </div>
        <div class="divUser" @click="toUserInfo">
            <img src="./../images/headPage.png"/>
            <div class="desc">
                <div class="divName">{{this.userInfo.name}}
                    <img :src="genderImage" v-if="this.userInfo.sex !== null"/>
                </div>
                <div class="divAccount">{{this.userInfo.account}}</div>
            </div>
        </div>
    </div>
    <div class="divContent">
        <div class="divLinks">
            <div class="item" @click="toAddressPage">
                <img src="./../images/locations.png"/>
                <span>地址管理</span>
                <div class="defaultAddress">
                    <div class="address">{{address.detail}}</div>
                    <div class="address">
                        <span>{{address.consignee}}{{address.sex === '0' ? '女士' : '先生'}}</span>
                        <span>{{address.phone}}</span>
                    </div>
                </div>
                <i class="el-icon-arrow-right"></i>
            </div>
            <div class="divSplit"></div>
            <div class="item" @click="toOrderPage">
                <img src="./../images/orders.png"/>
                <span>历史订单</span>
                <span class="order">{{getStatus(order[0].status)}}</span>
                <i class="el-icon-arrow-right"></i>
            </div>
        </div>
        <div class="divOrders" v-if="order[0]">
            <div class="title">最新订单</div>
            <div class="timeStatus">
                <span>{{order[0].orderTime}}</span>
                <span>{{getStatus(order[0].status)}}</span>
            </div>
            <div class="dishList">
                <div v-for="(item,index) in order[0].orderDetails" :key="index" class="item">
                    <span>{{item.name}}</span>
                    <span>x{{item.sumNum}}</span>
                </div>
            </div>
            <div class="result">
                <!--<span>共 1 件商品,实付</span>-->
                <span>共{{order[0].sumNum}}件商品,实付</span>
                <span class="price">￥{{order[0].amount}}</span>
            </div>
            <div class="btn" v-if="order[0].status === 4">
                <div class="btnAgain" @click="addOrderAgain(order[0])">再来一单</div>
            </div>
        </div>
        <div class="quitLogin" @click="toPageLogin">
            退出登录
        </div>
    </div>
</div>
<!-- 开发环境版本，包含了有帮助的命令行警告 -->
<script src="../../backend/plugins/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../../backend/plugins/element-ui/index.js"></script>
<!-- 引入vant组件 -->
<script src="./../js/vant.min.js"></script>
<!--不同屏幕尺寸根字体设置-->
<script src="./../js/base.js"></script>
<!-- 引入axios -->
<script src="../../backend/plugins/axios/axios.min.js"></script>
<script src="./../js/request.js"></script>
<script src="./../api/order.js"></script>
<script src="./../api/login.js"></script>
<script src="./../api/user.js"></script>
<script src="./../api/address.js"></script>
<script src="./../api/main.js"></script>
<script>
    new Vue({
        el: "#user",
        data() {
            return {
                address: {
                    phone: '',//手机号
                    consignee: '',//姓名
                    detail: '',//详细地址
                    sex: '',
                    id: undefined
                },
                form: {
                    account: '',
                    code: ''
                },
                msgFlag: false,
                order: [{
                    orderTime: '',//下单时间
                    status: '',//订单状态 1已结账，2未结账，3已退单，4已完成，5已取消
                    orderDetail: [{
                        name: '',//菜品名称
                        number: '',//数量
                    }],//明细
                    amount: '',//实收金额
                    sumNum: '',//菜品总数
                }],
                userInfo: {
                    id: '',
                    name: '',
                    account: '',
                    sex: '',
                    idNumber: '',
                    avatar: '',
                    status: ''
                }
            }
        },
        computed: {
            genderImage() {
                if (this.userInfo.sex === "1") {
                    return './../images/female.png';
                } else if (this.userInfo.sex === "0") {
                    return './../images/secrecy.png';
                } else {
                    return './../images/male.png';
                }
            }
        },
        created() {
            this.getUserInfo()
            this.initData()
            this.defaultAddress()
        },
        mounted() {
        },
        methods: {
            //修改用户信息
            async toUserInfo() {
                window.requestAnimationFrame(() => {
                    window.location.href = '/front/page/user-edit.html'
                })
            },
            //获取默认地址
            async defaultAddress() {
                const res = await getDefaultAddressApi()
                if (res.code === 1) {
                    this.address = res.data
                    this.getFinishTime()
                } else {
                    window.requestAnimationFrame(() => {
                        window.location.href = '/front/page/address-edit.html'
                    })
                }
            },
            async getUserInfo() {
                const res = await userInfoApi();
                if (res.code === 1) {
                    this.userInfo = res.data
                }
            },
            goBack() {
                history.go(-1)
            },
            toAddressPage() {
                window.requestAnimationFrame(() => {
                    window.location.href = '/front/page/address.html'
                })
            },
            toOrderPage() {
                window.requestAnimationFrame(() => {
                    window.location.href = '/front/page/order.html'
                })
            },
            initData() {
                this.getLatestOrder()
            },
            async getLatestOrder() {
                const params = {
                    page: 1,
                    pageSize: 1
                }
                const res = await orderPagingApi(params)
                if (res && res.code === 1 && res.data && res.data.records && res.data.records[0]) {
                    this.order = res.data.records
                    if (this.order[0] && this.order[0].orderDetail) {
                        let number = 0
                        this.order[0].orderDetail.forEach(item => {
                            number += item.number
                        })
                        this.order[0].sumNum = number
                    } else {
                        console.error('Order detail are missing!'); // 或其他适当的错误处理
                    }
                } else {
                    this.$notify({type: 'warning', message: res.msg});
                }
            },
            getStatus(status) {
                let str = ''
                switch (status) {
                    case 1:
                        str = '待付款'
                        break;
                    case 2:
                        str = '正在派送'
                        break;
                    case 3:
                        str = '已派送'
                        break;
                    case 4:
                        str = '已完成'
                        break;
                    case 5:
                        str = '已取消'
                        break;
                }
                return str
            },

            async addOrderAgain(order) {
                let res;
                const orderDetailList = order.orderDetail;
                for (const orderDetail of orderDetailList) {
                    const againOrder = {
                        name:orderDetail.name,
                        dishId:orderDetail.dishId,
                        dishFlavor:orderDetail.dishFlavor,
                        number:orderDetail.number,
                        amount:orderDetail.amount,
                        image:orderDetail.image,
                    }
                    res = await addCartApi(againOrder)
                }
                if (res.code === 1) {
                    window.requestAnimationFrame(() => {
                        window.location.href = '/front/index.html'
                    })
                }
            },
            async toPageLogin() {
                const res = await loginoutApi()
                if (res.code === 1) {
                    window.requestAnimationFrame(() => {
                        window.location.href = '/front/page/login.html'
                    })
                } else {
                    this.$notify({type: 'warning', message: res.msg});
                }
            },
            //获取送达时间
            getFinishTime() {
                let now = new Date()
                let hour = now.getHours() + 1
                let minute = now.getMinutes()
                if (hour.toString().length < 2) {
                    hour = '0' + hour
                }
                if (minute.toString().length < 2) {
                    minute = '0' + minute
                }
                this.finishTime = hour + ':' + minute
            }
        }
    })
</script>
</body>
</html>